package com.yanboo.generator.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.yanboo.common.annotation.EnableFilter;
import com.yanboo.common.config.UidGenerator;
import com.yanboo.generator.domain.DbDriver;
import com.yanboo.common.utils.ValidationUtil;
import com.yanboo.common.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.yanboo.generator.repository.DbDriverRepository;
import com.yanboo.generator.service.DbDriverService;
import com.yanboo.generator.service.dto.DbDriverDto;
import com.yanboo.generator.service.dto.DbDriverQueryCriteria;
import com.yanboo.generator.service.mapstruct.DbDriverMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.yanboo.common.utils.PageUtil;
import com.yanboo.common.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import jakarta.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import com.yanboo.common.utils.PageResult;

/**
 * @description 服务实现
 * @author fdq
 * @date 2023-12-01
 **/
@Service
@RequiredArgsConstructor
public class DbDriverServiceImpl implements DbDriverService {

    private final DbDriverRepository dbDriverRepository;
    private final DbDriverMapper dbDriverMapper;

    @Override
    @EnableFilter
    public PageResult<DbDriverDto> queryAll(DbDriverQueryCriteria criteria, Pageable pageable){
        Page<DbDriver> page = dbDriverRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(dbDriverMapper::toDto));
    }

    @Override
    public List<DbDriverDto> queryAll(DbDriverQueryCriteria criteria){
        return dbDriverMapper.toDto(dbDriverRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public List<DbDriverDto> queryAll(){
        return dbDriverMapper.toDto(dbDriverRepository.findAll());
    }

    @Override
    @Transactional
    public DbDriverDto findById(Long id) {
        DbDriver dbDriver = dbDriverRepository.findById(id).orElseGet(DbDriver::new);
        ValidationUtil.isNull(dbDriver.getId(),"DbDriver","id",id);
        return dbDriverMapper.toDto(dbDriver);
    }

    @Autowired
    UidGenerator uidGenerator;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(DbDriver resources) {
//        DbDriverQueryCriteria dbDriverQueryCriteria = new DbDriverQueryCriteria();
//        List<DbDriver> all = dbDriverRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, dbDriverQueryCriteria, criteriaBuilder));
//        if (all.isEmpty()){
//            resources.setId(uidGenerator.snowflakeId());
//            dbDriverRepository.save(resources);
//            return;
//        }
//        resources.setId(all.get(0).getId());
        dbDriverRepository.saveAndFlush(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DbDriver resources) {
        DbDriver dbDriver = dbDriverRepository.findById(resources.getId()).orElseGet(DbDriver::new);
        ValidationUtil.isNull( dbDriver.getId(),"DbDriver","id",resources.getId());
        dbDriver.copy(resources);
        dbDriverRepository.save(dbDriver);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            dbDriverRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<DbDriverDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DbDriverDto dbDriver : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" dbType",  dbDriver.getDbType());
            map.put(" driverClass",  dbDriver.getDriverClass());
            map.put(" driverPath",  dbDriver.getDriverPath());
            map.put(" name",  dbDriver.getName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
